package com.xishu.upgrade;

import com.xishu.config.Config;
import com.xishu.entity.plat.PrivilegeMapping;
import com.xishu.service.CommonService;
import com.xishu.service.UserService;
import com.xishu.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;

import static com.xishu.config.Constant.PRIVILEGE_SHOP_LEVEL;

public class UpgradeUserService implements Tools {
    private static Logger logger = LoggerFactory.getLogger(UpgradeUserService.class);
    private CommonService commonService = CommonService.getInstance();
    private UserService userService = UserService.getInstance();

    private static UpgradeUserService instance = new UpgradeUserService();

    private UpgradeUserService() {
    }

    public static UpgradeUserService getInstance() {
        return instance;
    }

    public void upgradeUserPosPrivilege() {
        if (!Config.getInstance().upgradeUserPosPrivilege()) {
            logger.info("do not upgrade user pos privilege");
            return;
        }

        PrivilegeMapping privilegeMapping = new PrivilegeMapping();
        privilegeMapping.setLevel(PRIVILEGE_SHOP_LEVEL);
        commonService.doAll(privilegeMapping, mappping -> {
            try {
                List<String> privilegeList = mappping.getPrivilegeList();
                if (isEmpty(privilegeList)) {
                    logger.info("privilege list is empty");
                    return;
                }

                LinkedHashSet<String> privilegeSet = new LinkedHashSet<>(mappping.getPrivilegeList());
                privilegeSet.add("sjzhgl_bdckdpzt");
                mappping.setPrivilegeList(new ArrayList<String>(privilegeSet));
                commonService.save(mappping);
            } catch (Exception e) {
                logger.error("e", e);
            }

        });

    }
}
